package br.org.acessobrasil.processoacessibilidade.dao;

import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import br.org.acessobrasil.persistencia.DataSource;
import br.org.acessobrasil.portal.util.MySqlConnection;
import br.org.acessobrasil.processoacessibilidade.dao.util.EntityManagerAutoCommit;

public class Inicializador {
	private static EntityManager emPro = null;
	private static EntityManager emCms = null;
	private static DataSource dataSourcePro = new DataSource();
	private static DataSource dataSourceCms = new DataSource();
	private static String baseUrl = "jdbc:mysql://localhost/";
	public static void iniciar(){
		
		Properties propertiesJpa = new Properties();
		propertiesJpa.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
		
		dataSourcePro.setDriverClassName("com.mysql.jdbc.Driver");
		dataSourcePro.setUsername("root");
		dataSourcePro.setPassword("admin1234");
		dataSourcePro.setUrl(baseUrl + "procacesso");
		dataSourcePro.criarBancoSempre();
		
		HibernateJpaVendorAdapter hibernateJpaVendorAdapterPro = new HibernateJpaVendorAdapter();
		hibernateJpaVendorAdapterPro.setDatabase(Database.MYSQL);
		hibernateJpaVendorAdapterPro.setShowSql(false);
		hibernateJpaVendorAdapterPro.setGenerateDdl(true);
		
		LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBeanPro =new LocalContainerEntityManagerFactoryBean();
		localContainerEntityManagerFactoryBeanPro.setDataSource(dataSourcePro);
		localContainerEntityManagerFactoryBeanPro.setPersistenceUnitName("processoAcessibilizar");
		localContainerEntityManagerFactoryBeanPro.setJpaVendorAdapter(hibernateJpaVendorAdapterPro);
		localContainerEntityManagerFactoryBeanPro.setJpaProperties(propertiesJpa);
		localContainerEntityManagerFactoryBeanPro.afterPropertiesSet();
		EntityManagerFactory factoryPro = localContainerEntityManagerFactoryBeanPro.getObject();  
		
		emPro = new EntityManagerAutoCommit(factoryPro.createEntityManager(),factoryPro);
		/*
		 * CMS 
		 */
		dataSourceCms.setDriverClassName("com.mysql.jdbc.Driver");
		dataSourceCms.setUsername("root");
		dataSourceCms.setPassword("admin1234");
		dataSourceCms.setUrl(baseUrl + "cms");
		dataSourceCms.criarBancoSempre();
		
		HibernateJpaVendorAdapter hibernateJpaVendorAdapterCms = new HibernateJpaVendorAdapter();
		hibernateJpaVendorAdapterCms.setDatabase(Database.MYSQL);
		hibernateJpaVendorAdapterCms.setShowSql(false);
		hibernateJpaVendorAdapterCms.setGenerateDdl(true);
		new MySqlConnection().setDataSource(dataSourceCms);
		LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBeanCms =new LocalContainerEntityManagerFactoryBean();
		localContainerEntityManagerFactoryBeanCms.setDataSource(dataSourceCms);
		localContainerEntityManagerFactoryBeanCms.setPersistenceUnitName("cms");
		localContainerEntityManagerFactoryBeanCms.setJpaVendorAdapter(hibernateJpaVendorAdapterPro);
		localContainerEntityManagerFactoryBeanCms.setJpaProperties(propertiesJpa);
		localContainerEntityManagerFactoryBeanCms.afterPropertiesSet();
		EntityManagerFactory factoryCms = localContainerEntityManagerFactoryBeanCms.getObject();  

		emCms = new EntityManagerAutoCommit(factoryCms.createEntityManager(),factoryCms);
	}
	
	

	
	public static Object getCmsDao(String classe){
		try{
			Object instancia = Class.forName(
					"br.org.acessobrasil.portal.persistencia." + classe)
					.newInstance();
			for (java.lang.reflect.Method metodo : instancia.getClass().getMethods()) {
				if(metodo.getName().equals("setEntityManager"))
					metodo.invoke(instancia, new Object[]{emCms});
			}
			return instancia;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Object getProDao(String classe){
		try{
			Object instancia = Class.forName(
					"br.org.acessobrasil.processoacessibilidade.dao." + classe)
					.newInstance();
			for (java.lang.reflect.Method metodo : instancia.getClass().getMethods()) {
				if(metodo.getName().equals("setEntityManager")){
					metodo.invoke(instancia, new Object[]{emPro});
				}
			}
			return instancia;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static DataSource getDataSource() {
		return dataSourcePro;
	}

}
